import 'package:common_utils/common_utils.dart';
import 'package:erp/main_state.dart';
import 'package:erp/res/resources.dart';
import 'package:erp/widgets/form1/field/filter_chip.dart';
import 'package:erp/widgets/load_image.dart';
import 'package:erp/widgets/my_app_bar.dart';
import 'package:erp/widgets/page_list/page_list.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_picker/Picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'controller.dart';
import 'widgets/sale_project_card.dart';
import 'widgets/search_header.dart';

export 'binding.dart';

class SaleProjectList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var controller = Get.find<SaleProjectListController>();
    var ms = Get.find<MainState>();
    return Scaffold(
      backgroundColor: Colours.bg_color,
      resizeToAvoidBottomInset: false,
      appBar: MyAppBar(
        isBack: true,
        centerTitle: '立项管理',
        titleColor: Colors.black,
        bottom: PreferredSize(
          preferredSize: Size.fromHeight(90.h),
          child: SearchHeader(
            onTapFilter: () {
              controller.showOverlay.value = !controller.showOverlay.value;
            },
            onTapSearch: (value) {
              controller.search(value);
            },
          ),
        ),
      ),
      body: Stack(
        children: [
          PageListWidget<dynamic, SaleProjectListController>(
            createItem: (BuildContext context, int index, dynamic item) {
              var bidStatus = ms.getTranslation("bid_status");
              item["bidStatusName"] = bidStatus[item["bidStatus"]];
              item["contractStatusName"] = controller.contractStatusNameMap[item["contractStatus"]];
              return Padding(
                padding: EdgeInsets.fromLTRB(32.w, 20.w, 32.w, 0),
                child: SaleProjectCard(
                  saleProject: item,
                  detailTap: controller.tapDetail,
                ),
              );
            },
          ),
          // 下拉菜单
          Obx(
            () => controller.showOverlay.value
                ? Positioned(
                    top: 0,
                    left: 0,
                    right: 0,
                    bottom: 0,
                    child: Stack(
                      children: [
                        Container(
                          decoration: BoxDecoration(
                            color: Color.fromRGBO(0, 0, 0, 0),
                          ),
                        ),
                        Positioned(
                          child: Container(
                            height: 960.h,
                            width: MediaQuery.of(context).size.width,
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.only(
                                bottomLeft: Radius.circular(20.r),
                                bottomRight: Radius.circular(20.r),
                              ),
                              boxShadow: [
                                BoxShadow(
                                  offset: Offset(0, 1.w),
                                  blurRadius: 4.r,
                                  spreadRadius: 0,
                                  color: Color.fromRGBO(54, 95, 179, 0.2),
                                )
                              ],
                            ),
                            child: Column(
                              children: [
                                Container(
                                  padding: EdgeInsets.symmetric(horizontal: 55.w),
                                  constraints: BoxConstraints(minHeight: 180.h),
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Text(
                                        '项目类别',
                                        style: TextStyle(
                                            color: Color(0xff333333), fontSize: 30.sp, fontWeight: FontWeight.bold),
                                      ),
                                      SizedBox(height: 30.h),
                                      Obx(
                                        () => _SingleFilterChip<String>(
                                          fontSize: 24.sp,
                                          items: controller.projectCategoryItems,
                                          onChange: (value) {
                                            controller.queryForm['projectCategory'] = value;
                                          },
                                          value: controller.queryForm['projectCategory'],
                                        ),
                                      ),
                                      SizedBox(height: 30.h),
                                      Divider(
                                        height: 1.h,
                                        color: Color(0xffeeeeee),
                                      ),
                                      SizedBox(height: 30.h),
                                      Text(
                                        '投标状态',
                                        style: TextStyle(
                                            color: Color(0xff333333), fontSize: 30.sp, fontWeight: FontWeight.bold),
                                      ),
                                      SizedBox(height: 30.h),
                                      Obx(
                                        () => _SingleFilterChip<String>(
                                          onChange: (value) {
                                            controller.queryForm['bidStatus'] = value;
                                          },
                                          fontSize: 24.sp,
                                          value: controller.queryForm['bidStatus'],
                                          items: controller.bidStatusItems,
                                          activeBackgroundColor: Color(0xffEBFFFE),
                                          activeTextColor: Color(0xff36D1C7),
                                        ),
                                      ),
                                      SizedBox(height: 30.h),
                                      Divider(
                                        height: 1.h,
                                        color: Color(0xffeeeeee),
                                      ),
                                      SizedBox(height: 30.h),
                                      Text(
                                        '合同状态',
                                        style: TextStyle(
                                            color: Color(0xff333333), fontSize: 30.sp, fontWeight: FontWeight.bold),
                                      ),
                                      SizedBox(height: 30.h),
                                      Obx(
                                        () => _SingleFilterChip<int>(
                                          onChange: (value) {
                                            controller.queryForm['contractStatus'] = value;
                                          },
                                          fontSize: 24.sp,
                                          value: controller.queryForm['contractStatus'],
                                          items: controller.contractStatusItems,
                                          activeBackgroundColor: Color(0xffFA8541),
                                          activeTextColor: Color(0xffFFF2EB),
                                        ),
                                      ),
                                      SizedBox(height: 30.h),
                                      Divider(
                                        height: 1.h,
                                        color: Color(0xffeeeeee),
                                      ),
                                      SizedBox(height: 28.h),
                                      Text(
                                        '立项日期',
                                        style: TextStyle(
                                            color: Color(0xff333333), fontSize: 30.sp, fontWeight: FontWeight.bold),
                                      ),
                                      SizedBox(height: 30.h),
                                      Container(
                                        width: 419.w,
                                        height: 60.h,
                                        decoration: BoxDecoration(
                                          border: Border.all(
                                              color: Color(0xFF999999), width: 1.w, style: BorderStyle.solid),
                                          borderRadius: BorderRadius.all(Radius.circular(30.r)),
                                        ),
                                        child: GestureDetector(
                                          onTap: () async {
                                            var adapter = DateTimePickerAdapter(
                                              type: PickerDateTimeType.kYM,
                                              isNumberMonth: true,
                                              yearSuffix: "年",
                                              monthSuffix: "月",
                                              daySuffix: "日",
                                            );
                                            await Picker(
                                              height: 220,
                                              itemExtent: 38,
                                              adapter: adapter,
                                              onConfirm: (Picker picker, List valueList) {
                                                var value = (picker.adapter as DateTimePickerAdapter).value;
                                                var str = DateUtil.formatDate(value, format: 'yyyy-MM');
                                                controller.queryForm['applyDate'] = str;
                                                controller.textEditingController.text = str;
                                              },
                                            ).showModal(context);
                                          },
                                          child: Row(
                                            children: [
                                              Expanded(
                                                child: TextField(
                                                  controller: controller.textEditingController,
                                                  enabled: false,
                                                  textAlign: TextAlign.center,
                                                  maxLines: 1,
                                                  textAlignVertical: TextAlignVertical.bottom,
                                                  decoration: InputDecoration(
                                                    isDense: true,
                                                    hintText: '请选择月份',
                                                    hintStyle: TextStyle(
                                                      color: Color(0xFFCCCCCC),
                                                      fontSize: 24.sp,
                                                      fontWeight: FontWeight.w400,
                                                    ),
                                                    contentPadding: EdgeInsets.only(left: 32.w),
                                                    border: InputBorder.none,
                                                    enabledBorder: InputBorder.none,
                                                    focusedBorder: InputBorder.none,
                                                  ),
                                                ),
                                              ),
                                              LoadAssetImage(
                                                'calendar',
                                                width: 43.w,
                                                height: 40.h,
                                              ),
                                              SizedBox(width: 30.w)
                                            ],
                                          ),
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                                // 底部按钮
                                Expanded(child: Container()),
                                Container(
                                  height: 80.h,
                                  child: Row(
                                    children: [
                                      Material(
                                        child: Ink(
                                          decoration: BoxDecoration(
                                            color: Colors.white,
                                            borderRadius: BorderRadius.only(bottomLeft: Radius.circular(20.r)),
                                          ),
                                          child: InkWell(
                                            onTap: () {
                                              print(controller.queryForm);
                                              controller.textEditingController.clear();
                                              controller.queryForm.keys.forEach((e) {
                                                controller.queryForm[e] = null;
                                              });
                                            },
                                            child: Container(
                                              alignment: AlignmentDirectional.center,
                                              height: 80.h,
                                              width: 375.w,
                                              child: Text(
                                                '重置',
                                                style: TextStyle(color: Color(0xff4990FF), fontSize: 36.sp),
                                              ),
                                            ),
                                          ),
                                        ),
                                      ),
                                      Material(
                                        child: Ink(
                                          decoration: BoxDecoration(
                                            color: Color(0xff4C8EFF),
                                            borderRadius: BorderRadius.only(bottomRight: Radius.circular(20.r)),
                                          ),
                                          child: InkWell(
                                            onTap: () {
                                              controller.showOverlay.value = false;
                                            },
                                            child: Container(
                                              alignment: AlignmentDirectional.center,
                                              height: 80.h,
                                              width: 375.w,
                                              child: Text(
                                                '确定',
                                                style: TextStyle(color: Colors.white, fontSize: 36.sp),
                                              ),
                                            ),
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  )
                : SizedBox.shrink(),
          ),
        ],
      ),
    );
  }
}

class _SingleFilterChip<T> extends StatelessWidget {
  final Color activeBackgroundColor;
  final Color backgroundColor;

  final Color textColor;
  final Color activeTextColor;

  final double fontSize;
  final List<FilterChipItem<T>> items;
  final void Function(T value) onChange;
  final T? value;

  _SingleFilterChip({
    Key? key,
    this.value,
    this.activeBackgroundColor = const Color(0xffEBF3FF),
    this.backgroundColor = const Color(0xffF5F7FA),
    this.textColor = const Color(0xff666666),
    this.activeTextColor = const Color(0xff4192FE),
    this.fontSize = 20,
    required this.onChange,
    required this.items,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    List<Widget> chips = [];
    for (FilterChipItem<T> item in items) {
      FilterChip chip = FilterChip(
        materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
        showCheckmark: false,
        selected: value == item.data,
        label: item.label,
        avatar: item.avatar,
        padding: item.contentPadding,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(30.r))),
        backgroundColor: backgroundColor,
        selectedColor: activeBackgroundColor,
        labelStyle: TextStyle(fontSize: fontSize, color: value == item.data ? activeTextColor : textColor),
        onSelected: (bool selected) {
          if (selected) {
            onChange(item.data);
          }
        },
      );
      chips.add(chip);
    }
    return Wrap(
      spacing: 12.w,
      runSpacing: 18.h,
      children: [...chips],
    );
  }
}
